package com.bs.rss.channel;

import java.util.Date;
import java.util.List;

import com.bs.rss.common.enums.EnumPagine;
import com.bs.rss.common.model.data.Channel;
import com.bs.rss.common.model.data.RessUser;
import com.bs.rss.common.model.data.Rss;
import com.bs.rss.common.model.data.User;
import com.bs.rss.common.utils.JsonReturn;
import com.bs.rss.common.utils.RSSLibJMain;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

public class ChannelController extends Controller{

	public void index(){
		
	}
	
	/**
	 * 添加频道
	 */
	@Before(Tx.class)
	public void add(){
		
		Channel c = getModel(Channel.class, "");
		
		User u =getSessionAttr("currentuser");
		
		c.setCtime(new Date());
		
		c.setCuser(u.getLoginname());
		
		c.save();
		
		c.setUrl(getRequest().getContextPath()+"/cha/xmlrss/"+c.getId());
		c.update();
		
		renderJson(JsonReturn.re.saveOk(EnumPagine.ok, c));
	}
	
	public void channels(){
		
		Page<Channel> lc = Channel.dao.findall(getParaToInt("pageIndex", 1), getParaToInt("pageSize", 10));
		renderJson(JsonReturn.re.PageOk(lc.getTotalRow(), lc, EnumPagine.ok));
		
	}
	public void channels$list(){
		
		renderJson(Ret.ok("list",Channel.dao.findall$list()));
	}
	
	public void upd(){
		
	}
	
	public void xmlrss(){
		int cid = getParaToInt();
		List<Rss> l = Rss.dao.findbychannelid(cid);
		Channel c = Channel.dao.findById(cid);
		String s = null;
		try {
			s =RSSLibJMain.dao.createXml(l, c);
		} catch (Exception e) {
			e.printStackTrace();
		}
		renderText(s);
	}

	/**
	 * 我的订阅
	 */
	public void mychannels(){
		User u = getSessionAttr("currentuser");
		Page<Record> lc = Channel.dao.findmy(getParaToInt("pageIndex", 1), getParaToInt("pageSize", 10), u.getId());
		renderJson(JsonReturn.re.PageOk(lc.getTotalRow(), lc, EnumPagine.ok));
	}
	/**
	 * 订阅
	 */
	public void Subscribe(){
		
		Integer id = getParaToInt("channel_id", 0);
		Integer typeid = getParaToInt("type_id", 0);
		User u = getSessionAttr("currentuser");
		
		boolean f = false;
		Ret t = JsonReturn.re.saveFail(EnumPagine.subfail);
		//订阅
		if(id!=0 && !isSubscribe(id, u.getId())){
			
			RessUser ru = new RessUser();
			ru.setChannelId(id);
			ru.setIsflag("0");
			ru.setRssTypeId(typeid);
			ru.setUserId(u.getId());
			f = ru.save();
			
			if(f){
				t = JsonReturn.re.saveOk(EnumPagine.subok, ru);
			}
		}else{
			t = JsonReturn.re.saveFail(EnumPagine.subed);
		}
		
		renderJson(t);
		
	}
	/**
	 * 退订
	 */
	public void UNSubscribe(){
		
		Integer id = getParaToInt("channel_id", 0);
		User u = getSessionAttr("currentuser");
		
		boolean f = false;
		Ret t = JsonReturn.re.saveFail(EnumPagine.unsubfail);
		
		if(id!=0 && isSubscribe(id, u.getId())){
			
			RessUser ru = RessUser.dao.findbyChannelandUser(id, u.getId());
		
			f = ru.delete();
			
			if(f){
				t = JsonReturn.re.saveOk(EnumPagine.unsubok, ru);
			}
		}else{
			
			t = JsonReturn.re.saveFail(EnumPagine.unsubed);
		}
		
		renderJson(t);
		
		
	}
	
	public void CheckSubscribe(){
		
		Integer id = getParaToInt("channel_id", 0);
		
		User u = getSessionAttr("currentuser");
	
		renderJson(Ret.create("flag", isSubscribe(id, u.getId())));
		
	}
	
	/**
	 * 检查用户是否已经预定该频道
	 * @param cid
	 * @param uid
	 * @return
	 */
	private static boolean isSubscribe(Integer cid, Integer uid){
		RessUser r = RessUser.dao.findbyChannelandUser(cid, uid);
		boolean flag = false;
		
		if(r != null){
			flag = true;
		}
		return flag;
	}
}
